var config = require("../../config");
Page({
  data: {
    EvaluateData: {    //评价数据
      ShowEvaluate: 0,
      evaluate: 5,     //星级评价
      items: [
        { status: false },
        { status: false },
        { status: false },
        { status: false },
        { status: false },
      ]
    },
    feedbackData: [   //反馈数据
      { text: "维修神速", show: false },
      { text: "质量靠谱", show: false },
      { text: "我愿意推荐给朋友", show: false },
      { text: "态度好服务棒", show: false }
    ],
    Dissatisfaction: [
      { text: "速度慢", show: false },
      { text: "不靠谱", show: false },
      { text: "服务态度不好", show: false }
    ]
  },
  onLoad(e) {
    this.setData({
      orderId:e.orderId
    });
  },
  onShareAppMessage() {

  },
  satisfactory(e) {    //满意
    this.setData({
      EvaluateData: {    //评价数据
        ShowEvaluate: 0,
        evaluate: 5,     //星级评价
        items: [
          { status: false },
          { status: false },
          { status: false },
          { status: false },
          { status: false },
        ]
      },
      feedbackData: [   //反馈数据
        { text: "维修神速", show: false },
        { text: "质量靠谱", show: false },
        { text: "我愿意推荐给朋友", show: false },
        { text: "态度好服务棒", show: false }
      ],
      Dissatisfaction: [
        { text: "速度慢", show: false },
        { text: "不靠谱", show: false },
        { text: "服务态度不好", show: false }
      ],
      textarea_value:''
    });
  },
  Unsatisfactory() {   //不满意
    this.setData({
      EvaluateData: {    //评价数据
        ShowEvaluate: 1,
        evaluate: 5,     //星级评价
        items: [
          { status: false },
          { status: false },
          { status: false },
          { status: false },
          { status: false },
        ]
      },
      feedbackData: [   //反馈数据
        { text: "维修神速", show: false },
        { text: "质量靠谱", show: false },
        { text: "我愿意推荐给朋友", show: false },
        { text: "态度好服务棒", show: false }
      ],
      Dissatisfaction: [
        { text: "速度慢", show: false },
        { text: "不靠谱", show: false },
        { text: "服务态度不好", show: false }
      ],
      textarea_value: ''      
    });
  },
  evaluate(e) {    //服务评价
    var item = e.currentTarget.dataset.index, num = 0;
    if (this.data.EvaluateData.items[item].status == true) {
      for (var i = 0; i < this.data.EvaluateData.items.length; i++) {
        if (i >= item) {
          this.data.EvaluateData.items[i].status = false;
        }
      }
    } else {
      for (var i = 0; i < item + 1; i++) {
        this.data.EvaluateData.items[i].status = true;
        num++;
      }
    }
    this.setData({
      EvaluateData: this.data.EvaluateData,
      num
    });
  },
  feedback(e) {    //反馈
    let td = this.data;
    var index = e.currentTarget.dataset.index;
    if (td.EvaluateData.ShowEvaluate == 0){
      td.feedbackData[index].show = !td.feedbackData[index].show;
      this.setData({
        feedbackData: td.feedbackData
      });
    }else{
      td.Dissatisfaction[index].show = !td.Dissatisfaction[index].show;
      this.setData({
        Dissatisfaction: td.Dissatisfaction
      });
    }
   

  },
  suggest(e) {
    this.setData({
      textarea: e.detail.value
    });

  },
  submit(e) {
    let td = this.data;
    let score = td.EvaluateData.ShowEvaluate == 0 ? 1 : 2;
    let order_detail_id = td.orderDetailId;
    let textarea = e.detail.value.textarea;
    let content = "";
    if (td.EvaluateData.ShowEvaluate == 0) {
      for (var i = 0; i < td.feedbackData.length; i++) {
        if (td.feedbackData[i].show == true) {
          content += "和" + td.feedbackData[i].text;
        }
      }
    } else {
      for (var i = 0; i < td.Dissatisfaction.length; i++) {

        if (td.Dissatisfaction[i].show == true) {
          content += "和" + td.Dissatisfaction[i].text;
        }
      }
    }
    if (td.num == 0 || td.num == undefined) {
      wx.showToast({
        title: "还没星级评价呢",
        icon: "loading",
        duration: 2000,
        mask: true
      });
    } else if (content == "") {
      wx.showToast({
        title: "还没反馈呢",
        icon: "loading",
        duration: 2000,
        mask: true
      });
    } else {
      submitData({
        orderId: td.orderId,
        score: score,
        num: td.num,
        content,
        advise: textarea
      });
    }

  }
});

function submitData(obj) {
  wx.getNetworkType({
    success: function (res) {
      if (res.networkType != "none") {
        wx.showLoading({
          title: '加载中...',
          mask: true
        });
        wx.request({
          method: 'POST',
          url: config.service.opinion,
          data: {
            uid: wx.getStorageSync("loginInfo").uid,
            logintoken: wx.getStorageSync("loginInfo").token,
            order_id: obj.orderId,
            comment_type: obj.score,
            start_comment: obj.num,
            comment_content: obj.content,
            advise: obj.advise
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res) {
            wx.hideLoading();
            if (res.data.code == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 2000,
                mask: true
              });
              setTimeout(function () {
                wx.navigateBack();
              }, 2200);
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "loading",
                duration: 2000,
                mask: true
              });
              setTimeout(function () {
                wx.navigateBack();
              }, 2200);
            }
          }
        });
      } else {
        wx.showToast({
          title: "好像没网了...",
          icon: "loading",
          duration: 2000,
          mask: true
        });
      }
    },
  });
}